package com.chefu.juche.controller;
import com.chefu.common.pojo.PageResult;
import com.chefu.juche.pojo.Merchant;
import com.chefu.juche.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商家Controller
 */

@RestController
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    /**
     * 分页查询商户
     */
    @GetMapping("/merchant/page")
    public ResponseEntity<PageResult<Merchant>> findMerchantByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "15") Integer rows,
            @RequestParam(value = "fullName", required = false) String fullName,
            @RequestParam(value = "shortName", required = false) String shortName,
            @RequestParam(value = "merchantId", required = false) Integer merchantId,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "isChain", required = false) Boolean isChain,
            @RequestParam(value = "status", required = false) Boolean status,

            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "auditStatus", required = false) String auditStatus

    ){
        PageResult<Merchant> pageResult = merchantService.findMerchantByAuditStatus(page,rows,fullName,merchantId,type,city,auditStatus,shortName,phone,isChain,status);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 根据商户ID查询商户
     */
    @GetMapping("/merchant")
    public ResponseEntity<Merchant> findMerchantByMerchantId(@RequestParam(value = "merchantId") Integer merchantId){
        Merchant merchant = merchantService.findMerchantByMerchantId(merchantId);
        return ResponseEntity.ok(merchant);
    }

    /**
     * 审核商户是否通过
     */
    @PutMapping("/merchant/audit")
    public ResponseEntity<Void> auditMerchant(@RequestParam(value = "merchantId") Integer merchantId, @RequestParam(value = "auditStatus") String auditStatus){
        merchantService.auditMerchant(merchantId,auditStatus);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 修改商家主体
     */
    @PutMapping("/merchant")
    public ResponseEntity<Void> saveMerchant(@RequestParam(value = "fullName", required = false)String fullName, @RequestParam(value = "shortName", required = false)String shortName, @RequestParam(value = "isChain", required = false)Boolean isChain, @RequestParam(value = "type", required = false)String type, @RequestParam(value = "linkman", required = false)String linkman, @RequestParam(value = "phone", required = false)String phone,@RequestParam(value = "city", required = false) String city, @RequestParam(value = "merchantId", required = false)int merchantId, @RequestParam(value = "address", required = false)String address,@RequestParam(value = "pic", required = false) String pic, @RequestParam(value = "tag", required = false)String tag){
        merchantService.updateMerchant(fullName,  shortName,  isChain,  type,  linkman, phone, city, merchantId,  address,  pic,  tag);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 添加商家主体
     */
    @PutMapping("/merchant")
    public ResponseEntity<Void> addMerchant(Merchant merchant){
        merchantService.saveMerchant(merchant);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }


    /**
     * 导出商家主体
     */
    @GetMapping("/merchant/export")
    public ResponseEntity<Void> export(Merchant merchant){
        merchantService.saveMerchant(merchant);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     *锁定商家主体
     */
    @PutMapping("/merchant/lock")
    public ResponseEntity<Void> lockMerchant( @RequestParam(value = "merchantId", required = false)int merchantId){
        merchantService.lockMerchantById(merchantId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }



}
